import request from "@/utils/request";
import {Message} from "element-ui";
import {Notification} from 'element-ui';

export default {
    namespaced: true,
    state: {
        userDate: {}
    },
    mutations: {
        setDate(state, payload) {
            state.userDate = payload;
        },
    },
    actions: {
        async login(context, payload) {
            try {
                if (payload.userName === "" || payload.password === "") {
                    Message({
                        showClose: true,
                        message: '用户名或密码不可为空！',
                        type: 'warning'
                    });
                    return;
                }
                const res = await request.get("/api/role/getRoleName/" + payload.userName);
                if (res.data !== "ROLE_admin") {
                    Message({
                        showClose: true,
                        message: '您没有权限！',
                        type: 'error'
                    });
                    return;
                }
                const response = await request({
                    method: 'POST',
                    url: '/api/login',
                    data: payload
                });
                if (!response.data.success) {
                    Message({
                        showClose: true,
                        message: response.data.message,
                        type: 'error'
                    });
                    return;
                }
                Notification({
                    title: '成功',
                    message: '登录成功！',
                    type: 'success',
                    duration: 3000
                });
                // 储存用户名
                localStorage.setItem('userName', response.data.data);
                setTimeout(() => {
                    location.href = "#/back/home";
                }, 1000);
            } catch (error) {
                Message({
                    showClose: true,
                    message: error.response.data.message,
                    type: 'error'
                });
            }
        },
        async isLogin(context) {
            let userName = localStorage.getItem('userName');
            const res = await request.get("/api/role/getRoleName/" + userName);
            if (userName == null || res.data !== "ROLE_admin") {
                return false;
            }
            try {
                const response = await request.get('/api/islogin');
                return response.data.success;
            } catch (error) {
                return false;
            }
        },
        async logout(context) {
            try {
                const response = await request.get("/api/logout");
                if (response.data.success) {
                    Message({
                        showClose: true,
                        message: '登出成功,等你回来！',
                        type: 'success'
                    });
                    localStorage.clear();
                    setTimeout(() => {
                        location.href = '/login';
                    }, 1000);
                }
            } catch (error) {
                Message({
                    showClose: true,
                    message: '发生意外！',
                    type: 'error'
                });
            }
        }
    }
}
